package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.*;
import cn.gson.boot.model.mapper.*;
import cn.gson.boot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ChRuKuService {
    @Autowired
    ChuKuMapper chuKuMapper;
    @Autowired
    ChuKuDao chuKuDao;
    @Autowired
    RuKuMapper ruKuMapper;
    @Autowired
    RuKuDao ruKuDao;
    @Autowired
    ChuKuXiangQInDao chuKuXiangQInDao;
    @Autowired
    RuKuXinagqinDao ruKuXinagqinDao;

    @Autowired
    TuiHuoXiangQinMapper tuiHuoXiangQinMapper;
    @Autowired
    ProducttDao producttDao;
    @Autowired
    TuiHuoDao tuiHuoDao;
    @Autowired
    HuanHuoDao huanHuoDao;
    @Autowired
    HuanHuoXiangQinMapper huanHuoXiangQinMapper;
    @Autowired
    WxgdTuiHuanDao wxgdTuiHuanDao;
    @Autowired
    DingDanXiangQingMapper dingDanXiangQingMapper;
    @Autowired
    DinDanDao dinDanDao;
    //退货(申请)入库
    public void tuiHuoRuKu(Integer thId , String empName){
        Ruku ruku = new Ruku();

        ruku.setRkOtherid(thId);
        ruku.setRkShuoming("退货入库");
        ruku.setRkYs(0);
        ruku.setRkSqtime(new Timestamp(new Date().getTime()));
        Ruku rukuSave = ruKuDao.save(ruku);

        //修改退货单的入库状态
        tuiHuoDao.updateTuiHuoRuku("z",thId);//z=已申请

        List<Tuihuoxiangqing> thxq = tuiHuoXiangQinMapper.ckTuiHuoXiangQin(thId);//退货详情
        Rukuxiangqin rkxq = null;
        for(int i=0;i<thxq.size();i++){
            rkxq = new Rukuxiangqin();
            rkxq.setRuku(rukuSave);
            rkxq.setProductt(thxq.get(i).getProductt());
            rkxq.setRkxqNum(thxq.get(i).getThcpNum());
            ruKuXinagqinDao.save(rkxq);
        }


    }

    @Autowired
    DinDanXiangQingDao dinDanXiangQingDao;
    @Autowired
    HuanHuoMapper huanHuoMapper;
    //换货入库
    public void huanHuoRuKu(Integer hhId, String empName) {
        Ruku ruku = new Ruku();

        ruku.setRkOtherid(hhId);
        ruku.setRkShuoming("换货入库");
        ruku.setRkYs(0);
        ruku.setRkSqtime(new Timestamp(new Date().getTime()));
        Ruku rukuSave = ruKuDao.save(ruku);

        //修改换货单的入库状态
        huanHuoDao.updateHuanHuoRuku("z",hhId);//z=已申请

        List<Huanhuoxiangqing> hhxq = huanHuoXiangQinMapper.ckHuanHuoXiangQin(hhId);//换货详情
        Rukuxiangqin rkxq = null;
        double ddMoney = 0;
        for(int i=0;i<hhxq.size();i++){
            rkxq = new Rukuxiangqin();
            rkxq.setRuku(rukuSave);
            rkxq.setProductt(hhxq.get(i).getProductt());
            rkxq.setRkxqNum(hhxq.get(i).getHhcpNum());
            ruKuXinagqinDao.save(rkxq);
            ddMoney += Double.parseDouble(hhxq.get(i).getHhcpXiaoji().toString());
        }

    }

    //查看所有出库
    public List<Chuku> selectAllChuKu(String ckHao,String ckTime) {
        return chuKuMapper.selectAllChuKu(ckHao,ckTime);
    }
    @Autowired
    JiaoFuJiHuaXiangQingMapper jiaoFuJiHuaXiangQingMapper;
    @Autowired
    JiaoFuJiHuaDao jiaoFuJiHuaDao;
    //交付计划申请出库
    public void jjsqChuku(Integer jjId) {
        Chuku ck = new Chuku();
        ck.setCkOtherid(jjId);
        ck.setCkShouming("交计出库");
        ck.setCkYs(0);
        ck.setCkSqtime(new Timestamp(new Date().getTime()));
        Chuku save = chuKuDao.save(ck);
        List<Jiaofujihuaxiangqing> jjxq = jiaoFuJiHuaXiangQingMapper.ckJJXQbyjjId(jjId);
        Chukuxiangqin ckxq = null;
        System.err.println("jjId:"+jjId);
        for(int i=0;i<jjxq.size();i++){
            ckxq = new Chukuxiangqin();
            ckxq.setChuku(save);
            ckxq.setProductt(jjxq.get(i).getProductt());
            ckxq.setCkxqNum(jjxq.get(i).getJfjhxqNum());
            chuKuXiangQInDao.save(ckxq);
        }

        jiaoFuJiHuaDao.updateJiaoFuJiHuaZt("待出库",jjId);
    }
    //换货生成的订单申请出库
    public void hhddChuku(Integer ddId) {
        Chuku ck = new Chuku();
        ck.setCkOtherid(ddId);
        ck.setCkShouming("换货订单出库");
        ck.setCkYs(0);
        ck.setCkSqtime(new Timestamp(new Date().getTime()));
        Chuku save = chuKuDao.save(ck);
        List<Dingdanxiangqing> jjxq = dingDanXiangQingMapper.ckDingDanXiangQing(ddId);
        Chukuxiangqin ckxq = null;
        for(int i=0;i<jjxq.size();i++){
            ckxq = new Chukuxiangqin();
            ckxq.setChuku(save);
            ckxq.setProductt(jjxq.get(i).getProductt());
            ckxq.setCkxqNum(jjxq.get(i).getDdcpNum());
            chuKuXiangQInDao.save(ckxq);
        }

        dinDanDao.updateHHDDchuKu(2,ddId);
    }

    //确定出库
    public void chukuOk(Chuku chuku) {
        chuKuDao.save(chuku);
        //根据出库id查看出库
        Chuku chukus = chuKuMapper.selectChuKuByCkId(chuku.getCkId());
        List<Chukuxiangqin> chxq = chukus.getChukuxiangqinList();
        for(int i=0;i<chxq.size();i++){
            producttDao.updateChuKuProductKuCun(chxq.get(i).getCkxqNum(),chxq.get(i).getProductt().getProducttId());
        }
        if(chuku.getCkShouming().equals("换货订单出库")){
            dinDanDao.updateHHDDchuKu(1,chuku.getCkOtherid());
        }else if(chuku.getCkShouming().equals("交计出库")){
            jiaoFuJiHuaDao.updateJiaoFuJiHuaZt("已出库",chuku.getCkOtherid());
        }
    }

    @Autowired
    WxgdTuiHuanMapper wxgdTuiHuanMapper;
    //换货确定出库
    public void huanHuoChukuOk(Chuku chuku) {
        Chuku save = chuKuDao.save(chuku);
        dinDanDao.updateHHDDchuKu(1,chuku.getCkOtherid());
        Chukuxiangqin chxq = null;
        System.err.println("chuku.getChukuxiangqinList().size():"+chuku.getChukuxiangqinList().size());
        for(int i=0;i<chuku.getChukuxiangqinList().size();i++){
            chxq = new Chukuxiangqin();
            chxq.setChuku(save);
            chxq.setProductt(chuku.getChukuxiangqinList().get(i).getProductt());
            chxq.setCkxqNum(chuku.getChukuxiangqinList().get(i).getCkxqNum());
            chuKuXiangQInDao.save(chxq);
            producttDao.updateChuKuProductKuCun(chuku.getChukuxiangqinList().get(i).getCkxqNum(),chuku.getChukuxiangqinList().get(i).getProductt().getProducttId());
        }
    }

    //查看所有入库
    public List<Ruku> selectAllRuKu(String rkHao,String rkTime) {
        return ruKuMapper.selectAllRuKu(rkHao,rkTime);
    }

    //确定入库
    public void rukuOk(Ruku ruku) {
        ruKuDao.save(ruku);

        if(ruku.getRkShuoming().equals("退货入库")){
            //修改退货单的入库状态
            tuiHuoDao.updateTuiHuoRuku("y",ruku.getRkOtherid());//y=已入库
        }else if(ruku.getRkShuoming().equals("换货入库")){
            huanHuoDao.updateHuanHuoRuku("y",ruku.getRkOtherid());//y=已入库
        }else if(ruku.getRkShuoming().equals("退货维修入库")){
            wxgdTuiHuanDao.updatewxtuihuanRuKuZtai(2,ruku.getRkOtherid());
            //查看所有退换货维修工单id找对象
            Wxgdtuihuan wt = wxgdTuiHuanMapper.selectwxTuiHuanBygd2Id(ruku.getRkOtherid());
            tuiHuoDao.updateTuiHuoRuku("y",wt.getGd2Thhhid());
            wxgdTuiHuanDao.updatewxtuihuanZhuangTai("结束",ruku.getRkOtherid());
        }else if(ruku.getRkShuoming().equals("换货维修入库")){
            wxgdTuiHuanDao.updatewxtuihuanRuKuZtai(2,ruku.getRkOtherid());
            //查看所有退换货维修工单id找对象
            Wxgdtuihuan wt = wxgdTuiHuanMapper.selectwxTuiHuanBygd2Id(ruku.getRkOtherid());
            huanHuoDao.updateHuanHuoRuku("y",wt.getGd2Thhhid());
            wxgdTuiHuanDao.updatewxtuihuanZhuangTai("结束",ruku.getRkOtherid());
        }

        //根据入库id查看入库
        Ruku rukus = ruKuMapper.selectRuKuByRkId(ruku.getRkId());
        List<Rukuxiangqin> rkxq = rukus.getRukuxiangqinList();
        for(int i=0;i<rkxq.size();i++){
            producttDao.updateRuKuProductKuCun(rkxq.get(i).getRkxqNum(),rkxq.get(i).getProductt().getProducttId());
        }
    }

    @Autowired
    DingDanMapper dingDanMapper;
    //出库 ：查看所有订单(执行中的订单)换货 没有出库的
    public List<Dingdan> chuKuSelectAllDingDan(){
        return dingDanMapper.chuKuSelectAllDingDan();
    }

    //维修工单(退换货)申请入库
    public void wxgdTuiHuanRuKu(Wxgdtuihuan wxgdtuihuan,HttpSession session) {
        Ruku ruku = new Ruku();
        ruku.setRkOtherid(wxgdtuihuan.getGd2Id());
        if(wxgdtuihuan.getGd2Thhh().equals("换货")){
            ruku.setRkShuoming("换货维修入库");
        }else if(wxgdtuihuan.getGd2Thhh().equals("退货")){
            ruku.setRkShuoming("退货维修入库");
        }
        ruku.setRkYs(0);
        ruku.setRkSqtime(new Timestamp(new Date().getTime()));
        Ruku rukuSave = ruKuDao.save(ruku);

        //修改（退换货）维修工单的入库状态
       wxgdTuiHuanDao.updatewxtuihuanRuKuZtai(1,wxgdtuihuan.getGd2Id());//判断是否入库了（0=>没有，1=>已申请,2=>已入库）

        List<Wxgdtuihuanxiangqin> hhxq = wxgdtuihuan.getWxgdtuihuanxiangqinList();//（退换货）维修工单详情
        Rukuxiangqin rkxq = null;
        for(int i=0;i<hhxq.size();i++){
            rkxq = new Rukuxiangqin();
            rkxq.setRuku(rukuSave);
            rkxq.setProductt(hhxq.get(i).getProductt());
            rkxq.setRkxqNum(hhxq.get(i).getGd2Num());
            ruKuXinagqinDao.save(rkxq);
        }
    }


    //根据入库id查看入库
    public Ruku selectRuKuByRkId(Integer rkId){
        return ruKuMapper.selectRuKuByRkId(rkId);
    }

}
